/***************************************************************************
 *   KShootout - online shootout card game.                                *
 *   Copyright (C) 2008, 2009  MacJariel                                   *
 *                                                                         *
 *   KShootout is free software: you can redistribute it and/or modify     *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   KShootout is distributed in the hope that it will be useful,          *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with KShootout.  If not, see <http://www.gnu.org/licenses/>.    *
 ***************************************************************************/
#ifndef __UTIL_H__
#define __UTIL_H__

/// @file util.h
/// This file contains miscellaneous useful functions.

#include <QString>
#include <QtGlobal>

/// \def NOT_REACHED()
///    The NOT_REACHED() macro prints fatal message, that code that was supposed
///    to be never reached was reached.
#define NOT_REACHED() qFatal("Fatal Error: NOT_REACHED triggered at line %d of %s", __LINE__, __FILE__)

/// \def NOT_IMPLEMENTED()
///    The NOT_IMPLEMENTED() macro prints warning message about executing
///    function that was not implemented yet.
#define NOT_IMPLEMENTED() qWarning("Warning: NOT_IMPLEMENTED triggered at line %d of %d", __LINE__, __FILE__)


/**
 * Sets a random seed for qrand() function.
 */
void randomize();

/**
 * Sets handlers for POSIX signals.
 */
void setSignalHandlers();

/**
 * Returns pseudo-random alphanumeric string of length between minLength and
 * maxLength.
 */
QString randomToken(int minLength, int maxLength);

/**
 * Returns true with probability given by the parameter.
 */
bool randomBool(qreal probability);

/**
 * Shuffles the container given in the argument. The container must support
 * count() and swap(index1, index2) methods.
 */
template <typename T, template<typename T> class C>
inline void shuffleList(C<T>& list)
{
    int size = list.count();
    int swapCount = size * 4;
    while (swapCount-- != 0) {
        list.swap(qrand() % size, qrand() % size);
    }
}

#endif // __UTIL_H__
